package com.ruoyi.system.util;


import com.ruoyi.system.domain.SchoolInfo;
import com.ruoyi.system.domain.ZzdOrderInfo;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 工具类
 */
public class Util {

    /**
     * 根据时间戳生成主键id
     * @return
     */
    public static String get_radom_id(){
     return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSSSSS")).toString();
    }

    /**
     * String转date类型
     * @param date_str
     * @return
     */
    public static Date fomat_date(String date_str){
        Date date = null;
        //实现将字符串转成⽇期类型
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(date_str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取所有站点信息
     * @param sql
     * @return
     * @throws SQLException
     */
    public static List<SchoolInfo> getOrgListData(String sql) throws SQLException, ClassNotFoundException {
        List<SchoolInfo> list = new ArrayList<SchoolInfo>();
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ruoyi_system", "root", "root123");
        Statement stmt =conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()){
            //将数据实体循环添加到list中
            SchoolInfo schoolInfo = new SchoolInfo();
            schoolInfo.setSchoolId(rs.getString("school_id"));
            schoolInfo.setSchoolName(rs.getString("school_name"));
            list.add(schoolInfo);
        }
        rs.close();
        stmt.close();
        conn.close();
        return list;
    }

    /**
     * 获取所有站点信息
     * @param sql
     * @return
     * @throws SQLException
     */
    public static List<ZzdOrderInfo> getStoreInfo(String sql) throws SQLException, ClassNotFoundException {
        List<ZzdOrderInfo> list = new ArrayList<>();
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ruoyi_system", "root", "root123");
        Statement stmt =conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        int columnCount = rs.getMetaData().getColumnCount() ;
        while (rs.next()){
            ZzdOrderInfo zzdOrderInfo = new ZzdOrderInfo();
            zzdOrderInfo.setCretTime(rs.getDate("cret_time"));
            zzdOrderInfo.setId(rs.getString("id"));
            zzdOrderInfo.setOrderBy(rs.getLong("order_by"));
            zzdOrderInfo.setOrderContent(rs.getString("order_content"));
            zzdOrderInfo.setOrderId(rs.getString("order_id"));
            zzdOrderInfo.setOrderName(rs.getString("order_name"));
            zzdOrderInfo.setOrderPrice(rs.getBigDecimal("order_price"));
            zzdOrderInfo.setOrderType(rs.getString("order_type"));
            zzdOrderInfo.setPicName(rs.getString("pic_name"));
            zzdOrderInfo.setPicPath(rs.getString("pic_path"));
            zzdOrderInfo.setPicType(rs.getString("pic_type"));
            zzdOrderInfo.setRemarks(rs.getString("remarks"));
            zzdOrderInfo.setStoreId(rs.getString("store_id"));
            zzdOrderInfo.setStoreName(rs.getString("store_name"));
            zzdOrderInfo.setUpdtTime(rs.getDate("updt_time"));
            list.add(zzdOrderInfo);
        }
        rs.close();
        stmt.close();
        conn.close();
        return list;
    }
}
